<?php

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Http\Requests\StoreCaseCategoryRequest;
use App\Http\Requests\UpdateCaseCategoryRequest;
use App\Models\CaseCategory;
use Illuminate\Http\Request;

class CaseCategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, CaseCategory $model)
    {
        $pid = $request->pid;

        $model = $model->select(['id', ...$model->fillable]);

        if (is_null($pid)) {
            return $model->get();
        }
        return $model->where('pid', (int)$pid)->get();
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoreCaseCategoryRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreCaseCategoryRequest $request)
    {
        return $this->successful(CaseCategory::create($request->all()));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\CaseCategory  $caseCategory
     * @return \Illuminate\Http\Response
     */
    public function show(CaseCategory $casesCategory)
    {
        return $this->successful($casesCategory);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\UpdateCaseCategoryRequest  $request
     * @param  \App\Models\CaseCategory  $caseCategory
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateCaseCategoryRequest $request, CaseCategory $casesCategory)
    {
        return $this->successful($casesCategory->update($request->all()));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\CaseCategory  $caseCategory
     * @return \Illuminate\Http\Response
     */
    public function destroy(CaseCategory $casesCategory)
    {
        $casesCategory->delete();
        return $this->successful('删除成功');
    }

    public function deleteMany(Request $request)
    {
        $ids = $request->ids;
        if (empty($ids)) {
            return $this->failed('请选择要删除的记录');
        }
        CaseCategory::destroy($ids);
        return $this->successful('删除成功');
    }
}
